package org.ssp.servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ssp.dao.OrderDao;
import org.ssp.dao.TravelDao;
import org.ssp.dao.UserDao;
import org.ssp.impl.OrderImpl;
import org.ssp.impl.TravelImpl;
import org.ssp.impl.UserImpl;
import org.ssp.pojo.Order;
import org.ssp.service.WXPayService;
import org.ssp.util.XML2Obj2XML;

/**
 * 微信支付成功，通知商户后台
 * 
 */
@WebServlet("/notifyhandle")
public class WXnotifyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		InputStream is=request.getInputStream();
		ByteArrayOutputStream outstream=new ByteArrayOutputStream();
		byte[] buffer=new byte[1024];
		int len=0;
		while((len=is.read(buffer))!=-1){
			outstream.write(buffer,0,len);
		}
		System.out.println("---wx支付成功------1");
		outstream.close();
		is.close();
		String result=new String(outstream.toByteArray(),"UTF-8");
		System.out.println("result:"+result);
	    Map<String, String> map;
		try {
			map = XML2Obj2XML.doXMLParse(result);
			if(map.get("result_code").toString().equalsIgnoreCase("SUCCESS")){
		    	if(WXPayService.verifyWeixinNotify(map)){//签名验证
		    		//订单处理，1,添加一条记录到order表   2,修改user的type
		    		Order order=new Order();
		    		order.setOrderId(map.get("out_trade_no"));
		    		order.setName(map.get("attach"));
		    		order.setPaytime(map.get("time_end"));
		    		order.setOpenid(map.get("openid"));
		    		order.setStatus(1);
		    		order.setTotal_fee(Integer.parseInt(map.get("total_fee")));
		    		OrderDao od=new OrderImpl();
		    		if(od.findByOid(order.getOrderId())==null){
			    		od.addOrder(order);
		    		}
		    		if(map.get("attach").equals("英盟MT")||map.get("attach").equals("英盟MT")||map.get("attach").equals("英盟MT")){
		    			System.out.println(map.get("time_end"));
			    		System.out.println(map.get("attach"));
			    		int type=0;
			    		if(map.get("attach").equals("英盟MT")){
			    			type=1;
			    		}else if(map.get("attach").equals("英盟ME")){
			    			type=2;
			    		}else if(map.get("attach").equals("英盟PLUS")){
			    			type=3;
			    		}
			    		//修改user type
			    		//免费会员：0   英盟MT： 1        英盟ME：2       英盟PLUS：3
			    		UserDao ud=new UserImpl();
			    		ud.setUserType(map.get("openid"), type);
		    		}else{//活动支付通知
		    			//订单处理：1，增加一条记录到order表，2：用户参加的活动中增加一条记录
		    			int id=Integer.parseInt(map.get("attach"));
		    			TravelDao td=new TravelImpl();
		    			td.insertTo_User_Travel(map.get("openid"),id);
		    		}
		    		
		    		// 告诉微信服务器，我收到信息了，不要在调用回调action了  
                    response.getWriter().write(XML2Obj2XML.setXML("SUCCESS", "OK")); 
		    	}
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
